from decimal import Decimal


class Staff:
    def __init__(self, job_number: str, name: str, bonus: Decimal):
        self._job_number = job_number
        self._name = name
        self._bonus = bonus

    def bonus(self):
        return self._bonus

    def to_csv_line(self):
        return f"{self._job_number},{self._name},{self._bonus:.2f} 元"

    @staticmethod
    def from_csv_line(line: str):
        s = line.split(",")
        assert len(s) == 3
        return Staff(s[0], s[1], Decimal(s[2].rstrip("元")))

    def __str__(self):
        return self.to_csv_line()


def _r1():
    with open("JJ.txt", "w") as file:
        while True:
            number = input("请输入工号（或输入 E 以结束）：")
            if number.lower() == "e":
                break
            name = input(f"请输入 {number} 的姓名：")
            bonus = Decimal(input(f"请输入 {name}（{number}） 的奖金："))
            staff = Staff(number, name, bonus)
            file.write(staff.to_csv_line())
            file.write("\n")


def _r2():
    with open("JJ.txt", "r") as file_input:
        staffs = [Staff.from_csv_line(line.rstrip("\n")) for line in file_input if line != ""]
    staffs.sort(key=Staff.bonus)
    with open("NewJJ.txt", "w") as file:
        for staff in staffs:
            file.write(staff.to_csv_line())
            file.write("\n")


def _main():
    _r1()
    _r2()


if __name__ == "__main__":
    _main()
